<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对多个数组或多维数组进行排序</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-merge.html">array_merge</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-pad.html">array_pad</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-multisort" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_multisort</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_multisort</span> &mdash; <span class="dc-title">对多个数组或多维数组进行排序</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-multisort-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>array_multisort</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$arr</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$arg</code><span class="initializer"> = SORT_ASC</span></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$arg</code><span class="initializer"> = SORT_REGULAR</span></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ]]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_multisort()</strong></span>
   可以用来一次对多个数组进行排序，或者根据某一维或多维对多维数组进行排序。
  </p>
  <p class="para">
   关联（<span class="type"><a href="language.types.string.html" class="type string">string</a></span>）键名保持不变，但数字键名会被重新索引。
  </p>
    
<p class="para">
 排序顺序标志：
 <ul class="itemizedlist">
  <li class="listitem">
   <span class="simpara"><strong><code>SORT_ASC</code></strong> - 按照上升顺序排序</span>
  </li>
  <li class="listitem">
   <span class="simpara"><strong><code>SORT_DESC</code></strong> - 按照下降顺序排序</span>
  </li>
 </ul>
</p>
<p class="para">
 排序类型标志：
 <ul class="itemizedlist">
  <li class="listitem">
   <span class="simpara"><strong><code>SORT_REGULAR</code></strong> - 将项目按照通常方法比较</span>
  </li>
  <li class="listitem">
   <span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - 将项目按照数值比较</span>
  </li>
  <li class="listitem">
   <span class="simpara"><strong><code>SORT_STRING</code></strong> - 将项目按照字符串比较</span>
  </li>
 </ul>
</p>
<p class="para">
 每个数组之后不能指定两个同类的排序标志。每个数组后指定的排序标志仅对该数组有效 - 在此之前为默认值
 <strong><code>SORT_ASC</code></strong> 和 <strong><code>SORT_REGULAR</code></strong>。
</p>
  
  <p class="para">
   输入数组被当成一个表的列并以行来排序——这类似于 SQL 的 ORDER BY
   子句的功能。第一个数组是要排序的主要数组。数组中的行（值）比较为相同的话就按照下一个输入数组中相应值的大小来排序，依此类推。
  </p>
  <p class="para">
   本函数的参数结构有些不同寻常，但是非常灵活。第一个参数必须是一个数组。接下来的每个参数可以是数组或者是下面列出的排序标志。
  </p>
  
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.array-multisort-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">arr</code></em></dt>

     <dd>

      <p class="para">
       要排序的一个 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">arg</code></em></dt>

     <dd>

      <p class="para">
       接下来的每个参数可以是另一个 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 或者是为之前 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 排序标志选项参数：
       <strong><code>SORT_ASC</code></strong>, 
       <strong><code>SORT_DESC</code></strong>, 
       <strong><code>SORT_REGULAR</code></strong>,
       <strong><code>SORT_NUMERIC</code></strong>,
       <strong><code>SORT_STRING</code></strong>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">...</code></em></dt>

     <dd>

      <p class="para">
       Additional <em><code class="parameter">arg</code></em>&#039;s. 
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

  
  
 <div class="refsect1 returnvalues" id="refsect1-function.array-multisort-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

  
 
 <div class="refsect1 examples" id="refsect1-function.array-multisort-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4867">
    <p><strong>Example #1 Sorting multiple arrays</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ar1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ar2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ar2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     In this example, after sorting, the first array will contain 0,
     10, 100, 100. The second array will contain 4, 1, 2, 3. The
     entries in the second array corresponding to the identical
     entries in the first array (100 and 100) were sorted as well.
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [0]=&gt; int(0)
  [1]=&gt; int(10)
  [2]=&gt; int(100)
  [3]=&gt; int(100)
}
array(4) {
  [0]=&gt; int(4)
  [1]=&gt; int(1)
  [2]=&gt; int(2)
  [3]=&gt; int(3)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4868">
    <p><strong>Example #2 排序多维数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ar&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"2"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_STRING</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ar</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_NUMERIC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_DESC</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     本例中在排序后，第一个数组将变成
     &quot;10&quot;，100，100，11，&quot;a&quot;（被当作字符串以升序排列）。第二个数组将包含
     1, 3, &quot;2&quot;, 2, 1（被当作数字以降序排列）。
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [0]=&gt; array(5) {
    [0]=&gt; string(2) &quot;10&quot;
    [1]=&gt; int(100)
    [2]=&gt; int(100)
    [3]=&gt; int(11)
    [4]=&gt; string(1) &quot;a&quot;
  }
  [1]=&gt; array(5) {
    [0]=&gt; int(1)
    [1]=&gt; int(3)
    [2]=&gt; string(1) &quot;2&quot;
    [3]=&gt; int(2)
    [4]=&gt; int(1)
  }
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4869">
    <p><strong>Example #3 对数据库结果进行排序</strong></p>
    <div class="example-contents"><p>
     本例中 <var class="varname"><var class="varname">data</var></var>
     数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。
    </p></div>
    <div class="example-contents"><p>
     例子中的数据如下：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
volume | edition
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7
</pre></div>
    </div>
    <div class="example-contents"><p>
     数据全都存放在名为 <var class="varname"><var class="varname">data</var></var>
     的数组中。这通常是通过循环从数据库取得的结果，例如
     <span class="function"><a href="function.mysql-fetch-assoc.html" class="function">mysql_fetch_assoc()</a></span>。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">67</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">86</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">85</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">98</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">86</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">67</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     本例中将把 <var class="varname"><var class="varname">volume</var></var> 降序排列，把
     <var class="varname"><var class="varname">edition</var></var> 升序排列。
    </p></div>
    <div class="example-contents"><p>
     现在有了包含有行的数组，但是 <span class="function"><strong>array_multisort()</strong></span>
     需要一个包含列的数组，因此用以下代码来取得列，然后排序。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;取得列的列表<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$volume</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'volume'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$edition</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'edition'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;将数据根据&nbsp;volume&nbsp;降序排列，根据&nbsp;edition&nbsp;升序排列<br />//&nbsp;把&nbsp;$data&nbsp;作为最后一个参数，以通用键排序<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$volume</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_DESC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$edition</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     数据集合现在排好序了，结果如下：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4870">
    <p><strong>Example #4 不区分大小写字母排序</strong></p>
    <div class="example-contents"><p>
     <strong><code>SORT_STRING</code></strong> 和
     <strong><code>SORT_REGULAR</code></strong>
     都是区分大小写字母的，大写字母会排在小写字母之前。
    </p></div>
    <div class="example-contents"><p>
     要进行不区分大小写的排序，就要按照原数组的小写字母拷贝来排序。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'Alpha'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'atomic'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Beta'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bank'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array_lowercase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'strtolower'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$array_lowercase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_STRING</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Alpha
    [1] =&gt; atomic
    [2] =&gt; bank
    [3] =&gt; Beta
)
</pre></div>
    </div>
   </div>
  </p>
  
      
  <p class="para">
   【译者注】本函数相当有用，为有助于理解，请再看下面这个例子：
  </p>
  <p class="para">
   <div class="example" id="example-4871">
    <p><strong>Example #5 名次排列</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$grade&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"score"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">70</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">95</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">70.0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">60</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"70"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"Zhang&nbsp;San"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Li&nbsp;Si"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Wang&nbsp;Wu"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Zhao&nbsp;Liu"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Liu&nbsp;Qi"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$grade</span><span style="color: #007700">[</span><span style="color: #DD0000">"score"</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_NUMERIC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_DESC</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;将分数作为数值，由高到低排序<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$grade</span><span style="color: #007700">[</span><span style="color: #DD0000">"name"</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_STRING</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;将名字作为字符串，由小到大排序<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$grade</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;score&quot;]=&gt;
  array(5) {
    [0]=&gt;
    int(95)
    [1]=&gt;
    string(2) &quot;70&quot;
    [2]=&gt;
    float(70)
    [3]=&gt;
    int(70)
    [4]=&gt;
    int(60)
  }
  [&quot;name&quot;]=&gt;
  array(5) {
    [0]=&gt;
    string(5) &quot;Li Si&quot;
    [1]=&gt;
    string(6) &quot;Liu Qi&quot;
    [2]=&gt;
    string(7) &quot;Wang Wu&quot;
    [3]=&gt;
    string(9) &quot;Zhang San&quot;
    [4]=&gt;
    string(8) &quot;Zhao Liu&quot;
  }
}
</pre></div>
    </div>
    <div class="example-contents"><p>
     本例中对包含成绩的数组 <var class="varname"><var class="varname">$grade</var></var>
     按照分数（score）由高到低进行排序，分数相同的人则按照名字（name）由小到大排序。排序后李四
     95 分为第一名，赵六 60 分为第五名没有异议。张三、王五和刘七都是
     70 分，他们的名次则由其姓名的字母顺序排列，Liu 在前，Wang 在后而
     Zhang 在最后。为了区别，三个 70
     分分别用了整数，浮点数和字符串来表示，可以在程序输出中清楚地看到它们排序的结果。
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-multisort-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.usort.html" class="function" rel="rdfs-seeAlso">usort()</a> - 使用用户自定义的比较函数对数组中的值进行排序</span></li>
    <li class="member"><a href="array.sorting.html" class="link">数组排序函数对比</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-merge.html">array_merge</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-pad.html">array_pad</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
